package com.sjtu.fx.test;

import java.io.DataInputStream;
import javafx.io.http.*;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.Scene;
import javafx.ext.swing.SwingButton;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.ext.swing.SwingTextField;
import org.json.JSONObject;
import java.io.OutputStream;
import java.io.InputStream;
import javafx.ext.swing.SwingLabel;


var field:SwingTextField = SwingTextField {
    columns: 10
    text: "bookname"
    editable: true
};

var field2:SwingTextField = SwingTextField {
    columns: 10
    text: "author"
    editable: true
};

var field3:SwingTextField = SwingTextField {
    columns: 10
    text: "description"
    editable: true
};

var field4:SwingTextField = SwingTextField {
    columns: 10
    text: "coverURL"
    editable: true
};

var field5:SwingTextField = SwingTextField {
    columns: 10
    text: "category"
    editable: true
};

var parentfield:SwingTextField = SwingTextField {
    columns: 10
    text: "categoryParent"
    editable: true
};

var field6:SwingTextField = SwingTextField {
    columns: 10
    text: "isbn"
    editable: true
};

var field7:SwingTextField = SwingTextField {
    columns: 10
    text: "publisher"
    editable: true
};

var field8:SwingTextField = SwingTextField {
    columns: 10
    text: "bookScore"
    editable: true
};

var field9:SwingTextField = SwingTextField {
    columns: 10
    text: "labels"
    editable: true
};

var field10:SwingTextField = SwingTextField {
    columns: 10
    text: "libraryName"
    editable: true
};

var field11:SwingTextField = SwingTextField {
    columns: 10
    text: "libraryAddress"
    editable: true
};

var field12:SwingTextField = SwingTextField {
    columns: 10
    text: "bookNumber"
    editable: true
};

var resultField:SwingLabel = SwingLabel {
    text: ""
}


var bookname:String= bind field.text;
var author:String = bind field2.text;
var description:String = bind field3.text;
var coverURL:String = bind field4.text;
var category:String = bind field5.text;
var parentcategory:String = bind parentfield.text;
var isbn:String = bind field6.text;
var publisher:String = bind field7.text;
var bookScore:String = bind field8.text;
var labels:String = bind field9.text;
var libraryName:String = bind field10.text;
var libraryAddress:String = bind field11.text;
var bookNumber:String = bind field12.text;
function sendHttp(){
    HttpRequest {

        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/AddBook"

       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("bookname",bookname);
                json.put("author",author);
                json.put("description",description);
                json.put("coverURL",coverURL);
                json.put("category",category);
                json.put("parentCategory", parentcategory);
                json.put("isbn",isbn);
                json.put("publisher",publisher);
                json.put("bookScore",bookScore);
                json.put("labels",labels);
                json.put("libraryName", libraryName);
                json.put("libraryAddress", libraryAddress);
                json.put("bookNumber",bookNumber);
                var temp:String = "obj={json.toString()}";
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }

        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                resultField.text = data.readLine();
            } finally {
                is.close();
            }
        }
    }.start();
}



Stage {
    title : "AddBok"
    scene: Scene {
        width: 400
        height: 400
        content: [VBox{
            content:[
               SwingLabel {
                    text: "Add Book Please\n"
                    font: Font {
                        //face: VERDANA,
                        //style: "BOLD",
                        size: 30
                    }
                },
            HBox{
                content:[SwingLabel{
                        text:"bookname:   "
                    },field]
            },
            HBox{
                content:[SwingLabel{
                        text:"author:     "
                    },field2]
            },
            HBox{
                content:[SwingLabel{
                        text:"description:   "
                    },field3]
            },
            HBox{
                content:[SwingLabel{
                        text:"coverURL:   "
                    },field4]
            },
            HBox{
                content:[SwingLabel{
                        text:"category:   "
                    },field5]
            },
            HBox{
                content:[SwingLabel{
                        text:"parent category:   "
                    },parentfield]
            },
            HBox{
                content:[SwingLabel{
                        text:"isbn:   "
                    },field6]
            },
            HBox{
                content:[SwingLabel{
                        text:"publisher:   "
                    },field7]
            },
            HBox{
                content:[SwingLabel{
                        text:"bookScore:   "
                    },field8]
            },
            HBox{
                content:[SwingLabel{
                        text:"Labels:   "
                    },field9]
            },
            HBox{
                content:[SwingLabel{
                        text:"libraryName:   "
                    },field10]
            },
            HBox{
                content:[SwingLabel{
                        text:"libraryAddress:   "
                    },field11]
            },
            HBox{
                content:[SwingLabel{
                        text:"bookNumer:   "
                    },field12]
            },
            SwingButton {
            text: "Click"
            action: function() {
                sendHttp();
            }
            },
            resultField
        ]
        }
         ]
    }
}
   